$(function () {
    $("#backPage").load("/html/backPage.html");
    getStateByCode();
})

// 查询所有小组
function findAll() {
    let data = {
        key: $("#selectKey").val(),
        startTime: $("#startTime").val(),
        endTime: $("#endTime").val(),
        stateCode: $.trim($("#selectState").val()),
        pageNum: $("#pageNum").val(),
        pageSize: $("#pageSize").val()
    };
    let result = jsonGetValue("/team/getSelectList", data);
    console.log(result);
    setAllData(result.data.list);
    $("#total").html(result.data.count);
}
function setAllData(data) {
    let html = "";
    for (let i=0; i<data.length;i++) {
        html += "<tr>\n" +
            "        <td>" + data[i].id + "</td>\n" +
            "        <td>" + data[i].teamCode + "</td>\n" +
            "        <td>" + data[i].teamName + "</td>\n" +
            "        <td>" + (data[i].teacherName == undefined ? "暂无" : data[i].teacherName)+ "</td>\n" +
            "        <td>" + (data[i].coachName == undefined ? "暂无" : data[i].coachName)+ "</td>\n" +
            "        <td>" + (data[i].leaderName == undefined ? "暂无" : data[i].leaderName)+ "</td>\n" +
            "        <td>" + (data[i].teamSize == undefined ? "0" : data[i].teamSize) + "</td>\n";
        if (data[i].stateCode == "normal") {
            html += "<td class='yes'><div onclick=\"changeShowState('" + data[i].teamCode + "','graduation')\">" +
                    "<span>" + data[i].teamState + "</span><span class='iconfont'>&#xe60d</span></div></td>\n";
        } else if (data[i].stateCode == "graduation") {
            html += "<td class='no'><div onclick=\"changeShowState('" + data[i].teamCode + "','normal')\">" +
                    "<span>" + data[i].teamState + "</span><span class='iconfont'>&#xe600</span></div></td>\n";
        }
        html += "    <td>" + data[i].createTime + "</td>\n" +
            "        <td>" + data[i].updateTime + "</td>\n";
        html += "    <td>\n" +
            "            <div onclick=\"distribute('"+ data[i].teamCode +"')\">" +
            '            <a href="#distribute-modal-box" style="text-decoration:none;">' +
            '            <i class="iconfont icon-distribute">&#xe696;</i></a></div>\n' +
            "            <div onclick=\"edit('"+ data[i].teamCode +"')\">" +
            '            <a href="#edit-modal-box" style="text-decoration:none;">' +
            '            <i class="iconfont icon-edit">&#xe608;</i></a></div>\n' +
            "            <div onclick=\"del('"+ data[i].teamCode +"')\">" +
            '            <a href="#del-modal-box" style="text-decoration:none;">' +
            '            <i class="iconfont icon-del">&#xe60b;</i></a></div>\n' +
            "        </td>\n" +
            "    </tr>";
    }
    $("#tbody").html(html)
    $("#tbody > tr > td").css({"vertical-align":"middle"})
    $("#tbody > tr > td:nth-child(8) > div").css({"cursor": "pointer","display": "flex","align-items": "center"})
    $("#tbody > tr > td:last-child").css({"display": "flex","justify-content": "flex-start"})
    $("#tbody > tr > td:last-child > div").css({"cursor": "pointer"})
    $("#tbody > tr > td:last-child > div").css({"margin-right":"10px"})
    myCheckAO();
}

// 修改小组状态
function changeShowState(teamCode,stateCode){
    let data = {
        teamCode: teamCode,
        stateCode:stateCode
    };
    let result = jsonPost("/team/updateState", data);
    console.log(result);
    if (result.data.data == 1) {
        page('go');
    } else {
        alert("修改状态失败");
    }
}

// 从码表获取状态
function getStateByCode() {
    let result = jsonGetValue("/codeTable/getCodeByType",{type:"team_state"});
    console.log(result);
    setStateDataByCode(result.data.data);
}
function setStateDataByCode(data) {
    let html = "<option value='all'>全部</option>";
    for (let i=0;i<data.length;i++){
        html += '<div><option value ="'+ data[i].code +'">'+ data[i].key +'</option></div>';
    }
    $("#selectState").html(html);
}

// 添加小组
function addTeam() {
    let teamCode =  $("#addTeamCode").val();
    let teamName = $("#addTeamName").val();
    let data = {
        teamCode:teamCode,
        teamName: teamName,
    };
    if (teamCode == null || teamCode == "") {
        $("#teamCodeTip").text("小组编号不能为空");
    } else if (teamName == null || teamName == "") {
        $("#teamCodeTip").text();
        $("#teamNameTip").text("小组名不能为空");
    } else {
        let result = jsonPost("/team/addTeam", data);
        console.log(result);
        if (result.data.data == "success") {
            $(".right").load("/html/team/team-list.html");
        } else if (result.result == "repeat"){
            $("#teamCodeTip").text("该小组已存在");
        }
    }
}

// 分配组员
function distribute(teamCode) {
    $("#checkTeamCode").val(teamCode);
    checkUserTeam(teamCode);
}
// 调用查询分组成员的方法
function checkUserTeam(teamCode) {
    getUserByTeamCode(teamCode);
    getUserItemByNoTeamCode();
}

// 添加组员
function addTeamMember(name) {
    let userList = getAddCheck(name);
    let teamCode = $("#checkTeamCode").val();
    let data = {codeTeam:teamCode,teamCode:teamCode,ids:userList};
    console.log(data);
    let result = jsonPostParam("/user/updateUserTeam",data);
    console.log(result);
    if (result.data.data == "success"){
        checkUserTeam(teamCode);
    }else {
        alert("操作失败");
    }
}
// 获取多选框选择的成员
function getAddCheck(name) {
    let userName = [];
    $("[name='"+name+"']:checkbox").each(function(i){
        if ($(this).prop("checked") == true){
            userName.push(parseInt($(this).val()));
        }
    })
    userName = JSON.stringify(userName);
    userName = userName.replace("[","").replace("]","");
    return userName;
}

// 移除组员
function removeTeamMember(name) {
    let userList = getDelCheck(name);
    let codeTeam = $("#checkTeamCode").val();
    let data = {codeTeam:codeTeam,teamCode:"暂无",ids:userList};
    let result = jsonPostParam("/user/updateUserTeam",data);
    console.log(result);
    if (result.data.data == "success"){
        checkUserTeam(codeTeam);
    }else {
        alert("操作失败");
    }
}
// 获取多选框选择的成员
function getDelCheck(name) {
    let userName = [];
    $("[name='"+name+"']:checkbox").each(function(i){
        if ($(this).prop("checked") == true){
            if ($(this).val() == $("#teamLeaderId").val()){
                removeLeader($("#teamLeaderId").val());
            }
            userName.push(parseInt($(this).val()));
        }
    })
    userName = JSON.stringify(userName);
    userName = userName.replace("[","").replace("]","");
    return userName;
}

// 删除小组组长
function removeLeader(id) {
    let data ={
        id:id,
        teamCode:$("#checkTeamCode").val()
    }
    let result = jsonPostParam("/team/updateLeader",data);
    console.log(result);
    if (result.data.data == "success"){

    }
}

// 弹出修改模态框
function edit(teamCode) {
    getUserByTeamCode(teamCode);
}
// 执行修改
function doEdit() {
    let teamCode =  $("#updateTeamCode").val();
    let teamName = $("#updateTeamName").val();
    let teamLeader = $("#updateTeamLeader").val();
    let teamTeacher = $("#updateTeacher").val();
    let teamCoach = $("#updateCoach").val();
    let data = {
        teamCode:teamCode,
        teamName: teamName,
        teamLeader:teamLeader,
        teamTeacher:teamTeacher,
        teamCoach:teamCoach
    };
    console.log(data);
     if (teamName == null || teamName == "") {
        $("#teamNameTip").text("小组名不能为空");
    } else {
        let result = jsonPost("/team/updateTeam", data);
        console.log(result);
        if (result.data.data == 1) {
            $(".right").load("/html/team/team-list.html");
        }
    }
}

// 弹出删除模态框
function del(teamCode) {
    findTeamByTeamCode(teamCode);
}
// 执行删除
function doDel() {
    let result = jsonPost("/team/delTeam",{teamCode:$.trim($("#delTeamCode").val())});
    console.log(result);
    if (result.data.data == 1) {
        $(".right").load("/html/team/team-list.html");
    }
}

// 根据小组编号获取小组
function findTeamByTeamCode(teamCode) {
    let result = jsonGetValue("/team/getTeamByTeamCode",{teamCode: teamCode});
    console.log(result);
    setTeamDataByTeamCode(result.data.data);
}
// 给修改和删除模态框赋值
function setTeamDataByTeamCode(data) {
    $("#updateTeamCode").val(data.teamCode);
    $("#updateTeamName").val(data.teamName);
    $("#updateTeacher").find("option[value='"+ data.teamTeacher +"']").prop("selected",true);
    $("#updateCoach").find("option[value='"+ data.teamCoach +"']").prop("selected",true);
    $("#updateTeamLeader").find("option[value='"+ data.teamLeader +"']").prop("selected",true);
    $("#delTeamCode").val(data.teamCode)
    if (data.teamSize != 0){
        $("#tips").html("当前小组:" + data.teamName + "有组员,你不能执行本次操作");
        $("#delBtn").hide();
        $("#closeBtn").hide();
    } else {
        $("#tips").html("是否删除小组:" + data.teamName  +"?");
        $("#delBtn").show();
        $("#closeBtn").show();
    }
}

// 根据小组编号获取组员
function getUserByTeamCode(teamCode) {
    let data = {teamCode: teamCode}
    let result = jsonGetValue("/user/getUserByTeamCode",data);
    console.log(result);
    setUserDataByTeamCode(result.data.data,teamCode);
    setUserItemDataByTeamCode(result.data.data);
}
// 给组长下拉菜单赋值
function setUserDataByTeamCode(data,teamCode) {
    let html = "<option value='all'>全部</option>";
    for (let i=0;i<data.length;i++){
        html += '<div><option value ="'+ data[i].userCode +'">'+ data[i].name +'</option></div>';
    }
    $("#updateTeamLeader").html(html);
    getTeacherList(teamCode);
}

// 获取所有班主任
function getTeacherList(teamCode) {
    let result = jsonGet("/user/getTeacherList");
    console.log(result);
    setTeacherListData(result.data.data,teamCode);
}
// 给班主任下拉菜单赋值
function setTeacherListData(data,teamCode) {
    let html = "<option value='all'>全部</option>";
    for (let i=0;i<data.length;i++){
        html += '<div><option value ="'+ data[i].userCode +'">'+ data[i].name +'</option></div>';
    }
    $("#updateTeacher").html(html);
    getCoachList(teamCode);
}

// 获取所有教练
function getCoachList(teamCode) {
    let result = jsonGet("/user/getCoachList");
    console.log(result);
    setCoachListData(result.data.data,teamCode);
}
// 给教练下拉菜单赋值
function setCoachListData(data,teamCode) {
    let html = "<option value='all'>全部</option>";
    for (let i=0;i<data.length;i++){
        html += '<div><option value ="'+ data[i].userCode +'">'+ data[i].name +'</option></div>';
    }
    $("#updateCoach").html(html);
    findTeamByTeamCode(teamCode);
}


// 分配已分组成员
function setUserItemDataByTeamCode(data) {
    let html = "";
    for (let i=0;i<data.length;i++){
        if (data[i].roleId == 5){
            $("#teamLeaderId").val(data[i].id);
        }
        html += "<div>\n" +
            "        <label>\n" +
            "              <input  type=\"checkbox\" name='checkTeam' value='"+ data[i].id +"'/>\n" +
            "              <span>"+ data[i].name +"</span>\n" +
            "        </label>\n" +
            "    </div>";
    }
    $("#TeamMember").html(html);

}

// 获取未分组成员
function getUserItemByNoTeamCode() {
    let result = jsonGet("/user/getUserItemByNoTeamCode");
    console.log(result);
    setUserItemDataByNoTeamCode(result.data.data);
}
// 分配未分组成员
function setUserItemDataByNoTeamCode(data) {
    let html = "";
    for (let i=0;i<data.length;i++){
        html += "<div>\n" +
            "        <label>\n" +
            "              <input type=\"checkbox\" name='checkNoTeam' value='"+ data[i].id +"'/>\n" +
            "              <span>"+ data[i].name +"</span>\n" +
            "        </label>\n" +
            "    </div>";
    }
    $("#NoTeamMember").html(html);
}

// 获取多选框选择的需要删除的小组
function getCheckTeam() {
    let teams = [];
    $("[name='checkOne']:checkbox").each(function(i){
        if ($(this).prop("checked") == true){
            teams.push(parseInt($(this).val()));
        }
    })
    teams = JSON.stringify(teams);
    teams = teams.replace("[","").replace("]","");
    return teams;
}
// 批量删除
function batchDeletion() {
    let teams = getCheckTeam();
    let data = {ids:teams}
    console.log(data);
    let param = jsonPostParam("/team/getCheckTeam", data);
    console.log(param);
    if (param.data.data == 1){
        for (let i=0; i<param.data.length;i++) {
            if (param.data[i].teamSize != 0){
                let team = param.data[i].teamName
                alert("你所选择的这些小组中,"+ team + "组员不为空");
            }
        }
    }
    let result = jsonPostParam("/team/batchDeletion", data);
    console.log(result);
    if (result.data.data == "success"){
        $(".right").load("/html/team/team-list.html");
    }
}

// 查询点击事件
function select() {
    page('home');
}

// 清空点击事件
function clearAll() {
    $("input[ type='text']").val("");
    $("input[ type='date']").val("");
    $("#selectState").val("all");
    $("#pageSize").val(5);
    $("#pageNum").val(1);
}